
package action;

import db.User;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 * Show one user.
 * @author Tomas CASTA
 */
@UrlBinding("/user.action")
public class ShowUserActionBean implements ActionBean {
     /* action context */
     private ActionBeanContext context;
     /* user */
     private User user;
     /* user age */
     private String age;

    public void setAge(String age) {
        this.age = age;
    }

    public String getAge() {
        return age;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }
     
     public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    /**
     * Show one user profile.
     * @return Resolution
     */
    @DefaultHandler
    public Resolution show() {
        user = new User();
        user.setDb(this.context.getServletContext());
        try {
            /* Parse user id from URI */
            int id = Integer.valueOf(context.getRequest().getParameter("id"));
            user.findById(id);
            
            /* Get user age */
            int ageInt = user.getAge();

            /* OK */
            if (ageInt != -1) {
                age = String.valueOf(user.getAge());
            }
            /* User does not set date of birth, set age as unknown */
            else {
                age = "Neznámý";
            }
            
        }
         catch (NumberFormatException e) {
            Logger.getLogger(DeleteMessageActionBean.class.getName()).log(Level.SEVERE, null, e);
            return new ForwardResolution("/index.action");
        }
   
        return new ForwardResolution("jsp/showUser.jsp");
    }

}
